using SS.core;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
namespace SS
{
	public class frmupdtPossition : Form
	{
		private IContainer components = null;
		private GroupBox groupBox1;
		private Label label3;
		private TextBox TxtGpName;
		private Label label4;
		private TextBox TxtGpNote;
		private ToolTip toolTip1;
		private Button tSBClose;
		private Button tSBSave;
		private Button tSBAdd;
		private ListView lvUnit;
		private ColumnHeader columnHeader1;
		private ColumnHeader columnHeader2;
		private ColumnHeader columnHeader3;
		private Button btnDel;
		private Func objFunc = new Func();
		public int gPID;
		public bool checkUpt = false;
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			this.components = new Container();
			ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(frmupdtPossition));
			this.groupBox1 = new GroupBox();
			this.label3 = new Label();
			this.TxtGpName = new TextBox();
			this.label4 = new Label();
			this.TxtGpNote = new TextBox();
			this.toolTip1 = new ToolTip(this.components);
			this.tSBClose = new Button();
			this.tSBSave = new Button();
			this.tSBAdd = new Button();
			this.lvUnit = new ListView();
			this.columnHeader1 = new ColumnHeader();
			this.columnHeader2 = new ColumnHeader();
			this.columnHeader3 = new ColumnHeader();
			this.btnDel = new Button();
			this.groupBox1.SuspendLayout();
			base.SuspendLayout();
			this.groupBox1.BackColor = Color.Transparent;
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.TxtGpName);
			this.groupBox1.Location = new Point(7, 6);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new Size(470, 58);
			this.groupBox1.TabIndex = 2;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Thông tin cơ bản";
			this.label3.AutoSize = true;
			this.label3.Location = new Point(62, 25);
			this.label3.Name = "label3";
			this.label3.Size = new Size(58, 15);
			this.label3.TabIndex = 7;
			this.label3.Text = "Tên vị trí :";
			this.TxtGpName.Location = new Point(123, 22);
			this.TxtGpName.Name = "TxtGpName";
			this.TxtGpName.Size = new Size(332, 21);
			this.TxtGpName.TabIndex = 8;
			this.TxtGpName.Leave += new EventHandler(this.TxtGpName_Leave);
			this.TxtGpName.KeyPress += new KeyPressEventHandler(this.TxtGpName_KeyPress);
			this.TxtGpName.Enter += new EventHandler(this.TxtGpName_Enter);
			this.label4.AutoSize = true;
			this.label4.Location = new Point(144, 147);
			this.label4.Name = "label4";
			this.label4.Size = new Size(55, 15);
			this.label4.TabIndex = 9;
			this.label4.Text = "Ghi chú :";
			this.TxtGpNote.Location = new Point(202, 145);
			this.TxtGpNote.Name = "TxtGpNote";
			this.TxtGpNote.Size = new Size(172, 21);
			this.TxtGpNote.TabIndex = 10;
			this.TxtGpNote.Leave += new EventHandler(this.TxtGpNote_Leave);
			this.TxtGpNote.KeyPress += new KeyPressEventHandler(this.TxtGpNote_KeyPress);
			this.TxtGpNote.Enter += new EventHandler(this.TxtGpNote_Enter);
			this.tSBClose.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.tSBClose.BackColor = SystemColors.Control;
			this.tSBClose.Cursor = Cursors.Hand;
			this.tSBClose.FlatAppearance.BorderColor = Color.Gray;
			this.tSBClose.FlatStyle = FlatStyle.System;
			this.tSBClose.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.tSBClose.ImageAlign = ContentAlignment.MiddleLeft;
//			this.tSBClose.ImeMode = ImeMode.NoControl;
			this.tSBClose.Location = new Point(376, 382);
			this.tSBClose.Name = "tSBClose";
			this.tSBClose.Size = new Size(101, 30);
			this.tSBClose.TabIndex = 57;
			this.tSBClose.Text = "Thoát  [ ESC ]";
			this.tSBClose.UseVisualStyleBackColor = false;
			this.tSBClose.Click += new EventHandler(this.tSBClose_Click);
			this.tSBSave.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.tSBSave.BackColor = SystemColors.Control;
			this.tSBSave.Cursor = Cursors.Hand;
			this.tSBSave.FlatAppearance.BorderColor = Color.Gray;
			this.tSBSave.FlatStyle = FlatStyle.System;
			this.tSBSave.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.tSBSave.ImageAlign = ContentAlignment.MiddleLeft;
//			this.tSBSave.ImeMode = ImeMode.NoControl;
			this.tSBSave.Location = new Point(119, 382);
			this.tSBSave.Name = "tSBSave";
			this.tSBSave.Size = new Size(108, 30);
			this.tSBSave.TabIndex = 56;
			this.tSBSave.Text = "Ghi nhận  [ F6 ]";
			this.tSBSave.UseVisualStyleBackColor = false;
			this.tSBSave.Click += new EventHandler(this.tSBSave_Click);
			this.tSBAdd.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.tSBAdd.BackColor = SystemColors.Control;
			this.tSBAdd.Cursor = Cursors.Hand;
			this.tSBAdd.FlatAppearance.BorderColor = Color.Gray;
			this.tSBAdd.FlatStyle = FlatStyle.System;
			this.tSBAdd.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.tSBAdd.ImageAlign = ContentAlignment.MiddleLeft;
			//this.tSBAdd.ImeMode = ImeMode.NoControl;
			this.tSBAdd.Location = new Point(8, 382);
			this.tSBAdd.Name = "tSBAdd";
			this.tSBAdd.Size = new Size(110, 30);
			this.tSBAdd.TabIndex = 55;
			this.tSBAdd.Text = "Thêm mới  [ F4 ]";
			this.tSBAdd.UseVisualStyleBackColor = false;
			this.tSBAdd.Click += new EventHandler(this.tSBAdd_Click);
			this.lvUnit.Activation = ItemActivation.OneClick;
			this.lvUnit.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.lvUnit.AutoArrange = false;
			this.lvUnit.CheckBoxes = true;
			this.lvUnit.Columns.AddRange(new ColumnHeader[]
			{
				this.columnHeader1,
				this.columnHeader2,
				this.columnHeader3
			});
			this.lvUnit.FullRowSelect = true;
			this.lvUnit.GridLines = true;
			this.lvUnit.LabelWrap = false;
			this.lvUnit.Location = new Point(7, 71);
			this.lvUnit.Name = "lvUnit";
			this.lvUnit.Size = new Size(469, 306);
			this.lvUnit.TabIndex = 68;
			this.lvUnit.UseCompatibleStateImageBehavior = false;
			this.lvUnit.View = View.Details;
			this.lvUnit.SelectedIndexChanged += new EventHandler(this.lvUnit_SelectedIndexChanged);
			this.lvUnit.Click += new EventHandler(this.lvUnit_Click);
			this.columnHeader1.Text = "";
			this.columnHeader1.Width = 20;
			this.columnHeader2.Text = "Mã số";
			this.columnHeader2.Width = 0;
			this.columnHeader3.Text = "Tên gọi";
			this.columnHeader3.Width = 425;
			this.btnDel.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.btnDel.BackColor = SystemColors.Control;
			this.btnDel.Cursor = Cursors.Hand;
			this.btnDel.FlatAppearance.BorderColor = Color.Gray;
			this.btnDel.FlatStyle = FlatStyle.System;
			this.btnDel.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnDel.ImageAlign = ContentAlignment.MiddleLeft;
			this.btnDel.Location = new Point(228, 382);
			this.btnDel.Name = "btnDel";
			this.btnDel.Size = new Size(92, 30);
			this.btnDel.TabIndex = 69;
			this.btnDel.Text = "Xoá bỏ [ F8 ]";
			this.btnDel.UseVisualStyleBackColor = false;
			this.btnDel.Click += new EventHandler(this.btnDel_Click);
			base.AutoScaleDimensions = new SizeF(7f, 15f);
			//base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(484, 416);
			base.Controls.Add(this.lvUnit);
			base.Controls.Add(this.label4);
			base.Controls.Add(this.TxtGpNote);
			base.Controls.Add(this.btnDel);
			base.Controls.Add(this.tSBClose);
			base.Controls.Add(this.tSBSave);
			base.Controls.Add(this.tSBAdd);
			base.Controls.Add(this.groupBox1);
			this.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			base.KeyPreview = true;
			base.MaximizeBox = false;
			this.MaximumSize = new Size(500, 450);
			base.MinimizeBox = false;
			this.MinimumSize = new Size(500, 450);
			base.Name = "frmupdtPossition";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterScreen;
			this.Text = " .: Các vị trí bày thuốc";
			base.Load += new EventHandler(this.FrmupdtGproduct_Load);
			base.KeyDown += new KeyEventHandler(this.FrmupdtUnitproduct_KeyDown);
			this.groupBox1.ResumeLayout(false);
			this.groupBox1.PerformLayout();
			base.ResumeLayout(false);
			base.PerformLayout();
		}
		public frmupdtPossition()
		{
			this.InitializeComponent();
		}
		private void tSBClose_Click(object sender, EventArgs e)
		{
			base.Close();
		}
		private void txtGpParent_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				this.TxtGpName.Focus();
			}
		}
		private void TxtGpName_Enter(object sender, EventArgs e)
		{
			this.TxtGpName.BackColor = Color.Aqua;
		}
		private void TxtGpName_Leave(object sender, EventArgs e)
		{
			this.TxtGpName.BackColor = Color.White;
		}
		private void TxtGpName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				this.TxtGpNote.Focus();
			}
		}
		private void TxtGpNote_Enter(object sender, EventArgs e)
		{
			this.TxtGpNote.BackColor = Color.Aqua;
		}
		private void TxtGpNote_Leave(object sender, EventArgs e)
		{
			this.TxtGpNote.BackColor = Color.White;
		}
		private void TxtGpNote_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\r')
			{
				this.tSBSave.PerformClick();
			}
		}
		private bool CheckName(string tempName, int tempCusID)
		{
			bool result = false;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			SqlCommand sqlCommand = new SqlCommand();
			sqlCommand.Connection = sqlConnection;
			sqlCommand.CommandText = string.Concat(new object[]
			{
				"SELECT TOP 1 UnitName FROM [TabUnitProduct] WHERE [UnitName] = '",
				tempName,
				"' AND [UnitID]<>",
				tempCusID
			});
			sqlConnection.Open();
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			if (sqlDataReader.HasRows)
			{
				result = true;
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		private void tSBSave_Click(object sender, EventArgs e)
		{
			if (this.TxtGpName.Text.Trim() == "")
			{
				MessageBox.Show("Bạn cần cung cấp tên gọi của vị trí!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				this.TxtGpName.Focus();
			}
			else
			{
				if (this.CheckName(this.TxtGpName.Text.Trim(), this.gPID))
				{
					MessageBox.Show("Tên vị trí này đã tồn tại!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					this.TxtGpName.Focus();
				}
				else
				{
					SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
					sqlConnection.Open();
					SqlCommand sqlCommand = new SqlCommand("ActionOnTabPossition", sqlConnection);
					sqlCommand.CommandType = CommandType.StoredProcedure;
					sqlCommand.Parameters.Add("@ID", SqlDbType.Int);
					sqlCommand.Parameters.Add("@Name", SqlDbType.NVarChar);
					sqlCommand.Parameters["@ID"].Value = this.gPID;
					sqlCommand.Parameters["@Name"].Value = this.TxtGpName.Text;
					sqlCommand.ExecuteNonQuery();
					sqlConnection.Close();
					sqlConnection.Dispose();
					Possition possition = new Possition();
					possition.loadToListView(this.lvUnit);
					this.checkUpt = true;
				}
			}
		}
		private void FrmupdtGproduct_Load(object sender, EventArgs e)
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
			Possition possition = new Possition();
			possition.loadToListView(this.lvUnit);
			Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
		}
		private void tSBAdd_Click(object sender, EventArgs e)
		{
			this.TxtGpName.Text = "";
			this.TxtGpNote.Text = "";
			this.gPID = 0;
			this.TxtGpName.Focus();
		}
		public void DeleteGProducts(int _GpID)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			SqlCommand sqlCommand = new SqlCommand("DELETE FROM TabUnitProduct WHERE  [UnitID] = @UnitID", sqlConnection);
			sqlCommand.Parameters.Add("@UnitID", SqlDbType.Int);
			sqlCommand.Parameters["@UnitID"].Value = _GpID;
			sqlConnection.Open();
			sqlCommand.ExecuteNonQuery();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		private void lvUnit_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				this.gPID = int.Parse(this.lvUnit.Items[this.lvUnit.FocusedItem.Index].SubItems[1].Text);
				Possition possition = new Possition();
				possition.getInfo(this.gPID.ToString(), this.TxtGpName);
			}
			catch
			{
			}
		}
		private void btnDel_Click(object sender, EventArgs e)
		{
			try
			{
				if (MessageBox.Show("Có phải bạn muốn xoá mục được chọn không?\r\n\r\nBấm YES nếu đồng ý xoá, Bấm NO để bỏ qua!", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					Possition possition = new Possition();
					for (int i = 0; i < this.lvUnit.Items.Count; i++)
					{
						if (this.lvUnit.Items[i].Checked)
						{
							if (this.CheckUnit(this.lvUnit.Items[i].SubItems[1].Text))
							{
								MessageBox.Show("Vị trí này đang được sử dụng trong giao dịch !\r\n\r\nBạn không thể xoá bỏ nó.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
								possition.loadToListView(this.lvUnit);
								return;
							}
							possition.del(this.lvUnit.Items[i].SubItems[1].Text);
						}
					}
					possition.loadToListView(this.lvUnit);
				}
			}
			catch
			{
			}
		}
		private bool CheckUnit(string tmpUnit)
		{
			bool result = false;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			try
			{
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = "select TOP 1 PID from TabProducts WHERE [possID] = " + tmpUnit;
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				if (sqlDataReader.HasRows)
				{
					result = true;
				}
				sqlDataReader.Close();
				sqlDataReader.Dispose();
			}
			catch
			{
			}
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		private void FrmupdtUnitproduct_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.F4)
			{
				this.tSBAdd.PerformClick();
			}
			else
			{
				if (e.KeyCode == Keys.F6)
				{
					this.tSBSave.PerformClick();
				}
				else
				{
					if (e.KeyCode == Keys.F8)
					{
						this.btnDel.PerformClick();
					}
					else
					{
						if (e.KeyCode == Keys.Escape)
						{
							base.Close();
						}
					}
				}
			}
		}
		private void lvUnit_Click(object sender, EventArgs e)
		{
			try
			{
				this.gPID = int.Parse(this.lvUnit.Items[this.lvUnit.FocusedItem.Index].SubItems[1].Text);
			}
			catch
			{
			}
		}
	}
}
